<?php
/**
 * @author Oliver Leisalu
 * @package WapBilling
 */
class WapBilling_Elisa extends WapBillingOperator {

	protected $serviceName;

	protected $contentProviderId;

	protected $sharedSecret;

	protected $currency;

	protected $wsdl;

	protected $url;

	public function pay( $price, $description, $uid = 0, $logId ) {

		try {

			$sc  = new SoapClient( $this->wsdl );

			$date = 			date('YmdHis');
			$sessionId = 		$_SERVER['HTTP_X_WAPSESSIONINFO'];
			$serviceName = 		$this->serviceName . ($description ? ' ' . $description : '');
			$price = 			round($price*100);

			$contentProviderId = $this->contentProviderId;
			$url = 				$this->url;
			$currency = 		'EUR';
			$requestType = 		'click';
			$preAuth = 			false;
			$hash = 			(md5	($date.'+'.$sessionId.'+'.$serviceName.'+'.$price.'+'.$url.'+'.$currency.'+'.$contentProviderId.'+'.$requestType.'+'.$this->sharedSecret.'+'.($preAuth ? 'TRUE' : 'FALSE')));
			
			$params = 'date:'.$date."\n".
				'sessionId:'.$sessionId."\n".
				'serviceName:'.$serviceName."\n".
				'price:'.$price."\n".
				'contentProviderId:'.$contentProviderId."\n".
				'url:'.$url."\n".
				'currency:'.$currency."\n".
				'requestType:'.$requestType."\n".
				'preAuth:'.$preAuth."\n".
				'hash:'.$hash."\n";
			
			$log = new DomDBLogger('billing_elisa');
			$logId = $log->log('pay-request', $params );
			
			$res = $sc->processPayment( $date, $sessionId, $serviceName, $price, $contentProviderId, $hash, $url, $currency, $requestType, $preAuth);
			
			$logId = $log->log('pay-result-'.$logId, $res );
			
			if( $res == '000 OK') {
				return 0;
			} else {
				$this->lastError = $res;
				return 1;
			}
		} catch( SoapFault $e ) {
			return 3;
		}
	}

	private function getErrorMessages() {
		return array(
		901=>'SESSION NOT FOUND',
		902=>'SESSION INFO CORRUPTED',
		903=>'HASH MISMATCH',
		910=>'UNKNOWN CURRENCY',
		911=>'NOT ENOUGH CREDIT',
		912=>'PROVIDER CHARGE LIMIT EXCEEDED',
		913=>'SESSION COOKIE ALREADY CHARGED',
		914=>'FORBIDDEN REQUEST TYPE',
		920=>'UNKNOWN CONTENT PROVIDER',
		921=>'UNSUPPORTED OPERATOR',
		999=>'SYSTEM ERROR',
		922=>'INCORRECT REQUEST TYPE',
		000=>'OK'
		);
	}


	public function getMsisdn() {
		return $_SERVER['HTTP_X_MSISDN'];
	}

}
?>